from Autotests.LocalMachine.Pages.BasePage import BaseWebManagerPage
from Autotests.LocalMachine.UiObjectsMap.AirManager import Login
from Autotests.LocalMachine.Controls import *
from Autotests.WebDriver import DRIVER
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys


class WebLoginPage(BaseWebManagerPage):
    def __init__(self):
        super(WebLoginPage, self).__init__(Login.loginPage)
        self._textUserName = WebLineEdit(Login.txtLogin)
        self._textPassword = WebLineEdit(Login.txtPassword)
        self._buttonSignIn = WebButton(Login.btnSignIn)

    def goToHomeLocation(self):
        super(WebLoginPage, self).goToHomeLocation()
        DRIVER().switch_to_frame(DRIVER().find_element(*Login.loginFrame))

    @BaseWebManagerPage.setter
    def performLogin(self, login, password):
        self._textUserName.typeText(login)
        self._textPassword.typeText(password)
        self._buttonSignIn.send_keys(Keys.RETURN)
        while WebButton(Login.btnAllow).isExists():
            WebButton(Login.btnAllow).send_keys(Keys.RETURN)
            time.sleep(1)


